package 算法.leetcode.offer.nk;

/**
 * @author lchenglong
 * @date 2022/3/26
 */
public class BM18 {

    public static void main(String[] args) {
        BM18 instance = new BM18();
        int[][] array = new int[][]{
                {1, 2, 8, 9},
                {2, 4, 9, 12},
                {4, 7, 10, 13},
                {6, 8, 11, 15}
        };
        boolean find = instance.Find(6, array);
        System.out.println(find);
    }

    public boolean Find(int target, int[][] array) {
        if (array == null || array[0].length == 0) return false;
        int len = Math.min(array.length, array[0].length);
        for (int j = len - 1; j >= 0; j--) {
            if (array[j][j] == target) return true;
            else if (array[j][j] < target) {
                    for (int i = j; i < array.length; i++) {
                        if (array[i][j] == target) return true;
                        if (array[i][j] > target) break;
                    }
                    for (int i = j; i < array[0].length; i++) {
                        if (array[j][i] == target) return true;
                        if (array[j][i] > target) break;
                    }
            }
        }
        return false;
    }
}
